package com.zjun.demo.experimentaldemo.bluetooth_test


import com.zjun.demo.experimentaldemo.R
import com.zjun.demo.experimentaldemo.base.BaseActivity
import com.zjun.demo.experimentaldemo.databinding.ActivityBluetoothTestBinding
import java.nio.charset.StandardCharsets

class BluetoothTestActivity : BaseActivity<ActivityBluetoothTestBinding>() {

    companion object{

        const val TAG = "BluetoothTestActivity"

    }

    val dataStr16 = "59484b0001000000660000000a7b22636f646522"

    val dataStr161 = "59484b0001000000660000009d7b22636f646522"

    override fun getLayoutId(): Int = R.layout.activity_bluetooth_test

    override fun initialize() {
        val size1 = dataStr16.length
        val size2 = dataStr161.length
        val dsfdsf1 = hexToAscii(dataStr16)
        val dsfdsf2 = hexToAscii(dataStr161)
        dsfdsf1
    }

    fun hexStringToByteArray(s: String): ByteArray {
        val len = s.length
        val data = ByteArray(len / 2)
        var i = 0
        while (i < len) {
            data[i / 2] = ((s[i].digitToIntOrNull(16) ?: -1 shl 4)
            + s[i + 1].digitToIntOrNull(16)!! ?: -1).toByte()
            i += 2
        }
        return data
    }

    fun hexToAscii(hexStr: String): String? {
        val output = StringBuilder()
        var i = 0
        while (i < hexStr.length) {
            val str = hexStr.substring(i, i + 2)
            output.append(str.toInt(16).toChar())
            i += 2
        }
        return output.toString()
    }

}